﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Core
{
    public class RawImageEx : BaseUI
    {
        private RawImage _rawImg;

        [HideInInspector]
        public RawImage rawImg
        {
            get
            {
                if (_rawImg == null)
                    _rawImg = GetComponent<RawImage>();
                return _rawImg;
            }
        }

        private IAssetLoader assetLoader;

        internal string assetPath;

        private string prefabPath;
        private int width;
        private int height;
        private Vector3 cameraOffset;
        private bool orthographic;
        private Quaternion rotation;

        private bool isNeedBind;

        private void OnEnable()
        {
            if (isNeedBind)
                ChangeTexture(prefabPath, width, height, cameraOffset, orthographic, rotation);
        }

        public void ChangeTexture(string prefabPath)
        {
            var rect = GetComponent<RectTransform>();
            ChangeTexture(prefabPath, (int)rect.sizeDelta.x, (int)rect.sizeDelta.y, new Vector3(0, 0, -3), true, Quaternion.identity);
        }

        public async void ChangeTexture(string prefabPath, int width, int height, Vector3 cameraOffset, bool orthographic, Quaternion rotation)
        {
            this.prefabPath = prefabPath;
            this.width = width;
            this.height = height;
            this.cameraOffset = cameraOffset;
            this.orthographic = orthographic;
            this.rotation = rotation;

            if (!gameObject.activeInHierarchy)
            {
                isNeedBind = true;
                return;
            }

            isNeedBind = false;

            if (rawImg == null)
            {
                Debug.LogError("RawImage组件为空");
                return;
            }

            if (assetLoader == null)
                assetLoader = GetComponentInParent<IAssetLoader>();

            if (assetLoader == null)
            {
                Debug.LogError("找不到资源加载器");
                return;
            }

            var cameraData = new ChangeRawImageExTextureAsync.CameraData
            {
                localPosition = cameraOffset,
                orthographic = orthographic,
                rotation = rotation,
            };

            var asyncOperation = await new ChangeRawImageExTextureAsync(assetLoader, prefabPath, width, height, cameraData, this)
                .PublishAsync();

            if (gameObject == null)
                return;

            rawImg.texture = asyncOperation.texture;
            assetPath = prefabPath;
        }

        private void OnDestroy()
        {
            if (!string.IsNullOrEmpty(assetPath))
                new OnRawImageExDestroy(this).Publish();
        }
    }

    internal struct OnRawImageExDestroy : IEventData
    {
        public RawImageEx rawImageEx;

        public OnRawImageExDestroy(RawImageEx rawImageEx)
        {
            this.rawImageEx = rawImageEx;
        }
    }
}


